﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.codeplex.com/CompositeWPF"
    xmlns:Extensibility="clr-namespace:ScrumSprintMonitor.Extensibility;assembly=ScrumSprintMonitor.Extensibility"
    xmlns:converters="clr-namespace:ScrumSprintMonitor.UI.Wpf.Converters;assembly=ScrumSprintMonitor.UI.Wpf"
    xmlns:ViewModels="clr-namespace:ScrumSprintMonitor.Modules.WorkItemMonitor.ViewModels"
    xmlns:Progress="clr-namespace:Progress;assembly=ScrumSprintMonitor.UI.Wpf">
    
    <converters:DateTimeValueConverter x:Key="DateTimeValueConverter"/>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

    <DataTemplate x:Key="ExceptionDataTemplate">
        <TextBlock Foreground="White" TextWrapping="Wrap" FontSize="56pt" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding ExceptionMessage}" />
    </DataTemplate>

    <DataTemplate x:Key="WorkItemNotEstimatedErrorDataTemplate">
        <TextBlock Foreground="White" TextWrapping="Wrap" FontSize="56pt" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding StringFormat=Backlog item #\{0\} contains no estimates for remaining work., Path=Exception.WorkItem.Id}" />
    </DataTemplate>

    <DataTemplate x:Key="FetchingWorkItemsDataTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            
            <Rectangle Grid.RowSpan="3" Fill="Black" Opacity="0.5" />
            <Viewbox Grid.Row="1" HorizontalAlignment="Center">
                <Progress:CircularProgressBar/>
            </Viewbox>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="ErrorDataTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <ContentPresenter Grid.Row="0" Content="{Binding}" ContentTemplate="{StaticResource ExceptionDataTemplate}" Focusable="False" />
            
            <TextBlock Margin="5" Grid.Row="1" Text="{Binding Path=DeploymentVersion, StringFormat='Version: {0}'}" HorizontalAlignment="Center" Foreground="#7FFFFFFF" />
        </Grid>
    </DataTemplate>
    
    <DataTemplate x:Key="OnlineDataTemplate">
        <Grid>
            <!-- Background -->
            <ContentControl cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.PrimaryMonitorBackgroundRegion}" Focusable="False" />

            <DockPanel LastChildFill="True">
                <StackPanel DockPanel.Dock="Bottom" Margin="5">
                    <ItemsControl Grid.Column="0" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.PrimaryMonitorBottomDockRegion}" Focusable="False" />
                    
                    <Grid Margin="0,5,0,0" VerticalAlignment="Bottom">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <ContentControl Grid.Column="0" VerticalAlignment="Bottom" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.BuildStatusDetailsRegion}" Focusable="False" />

                        <TextBlock Grid.Column="1" VerticalAlignment="Bottom" Text="{Binding Path=DeploymentVersion, StringFormat='Version: {0}'}" HorizontalAlignment="Center" Foreground="#7FFFFFFF" />

                        <Grid Grid.Column="2" VerticalAlignment="Bottom">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>

                            <Grid Grid.Column="1" x:Name="LastRefreshArea">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="4"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>

                                <TextBlock Grid.Column="0" Text="Last update:" HorizontalAlignment="Right" Foreground="#7FFFFFFF" Opacity="0.5"/>
                                <TextBlock Grid.Column="2" Text="{Binding Path=LastUpdate, Converter={StaticResource DateTimeValueConverter}, ConverterParameter=G}" Foreground="#7FFFFFFF" />
                                <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal">
                                    <Image Source="..\Resources\VistaICO\Refresh.png" Stretch="Uniform" Height="16" />

                                    <TextBlock Margin="3,0,0,0" Text="Last refresh:" HorizontalAlignment="Right" Foreground="#7FFFFFFF" Opacity="0.5"/>
                                </StackPanel>
                                <TextBlock Grid.Column="2" Grid.Row="1" Text="{Binding Path=LastRefresh, Converter={StaticResource DateTimeValueConverter}, ConverterParameter=G}" Foreground="#7FFFFFFF" />
                            </Grid>
                            
                            <Button Grid.Column="0" Grid.ColumnSpan="2" Command="{Binding RefreshCommand}" Opacity="0.001" Grid.ZIndex="100">
                                <Button.Triggers>
                                    <EventTrigger RoutedEvent="Button.MouseEnter">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                    <EventTrigger RoutedEvent="Button.MouseLeave">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.001" Duration="0:0:1" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </Button.Triggers>

                                <Viewbox Margin="6">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>

                                        <Image Grid.Column="0" Margin="6" Source="..\Resources\VistaICO\Refresh.png" Stretch="Uniform" Height="32" />
                                        <TextBlock Grid.Column="1" Margin="6" VerticalAlignment="Center" Text="Refresh!" />
                                    </Grid>
                                </Viewbox>
                            </Button>
                        </Grid>
                    </Grid>
                </StackPanel>

                <Grid DockPanel.Dock="Top">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <!-- Left column -->
                    <ItemsControl Grid.Column="0" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.PrimaryMonitorLeftDockRegion}" Focusable="False">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>

                    <!-- Sprint Overview Region -->
                    <Grid Grid.Column="1" x:Name="SprintOverviewRegion" Margin="20">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition>
                                <ColumnDefinition.Style>
                                    <Style TargetType="{x:Type ColumnDefinition}">
                                        <Setter Property="Width" Value="0"/>

                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding SprintTitleOrientation}" Value="Vertical">
                                                <Setter Property="Width" Value="1*"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ColumnDefinition.Style>
                            </ColumnDefinition>
                            <ColumnDefinition Width="7*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition>
                                <RowDefinition.Style>
                                    <Style TargetType="{x:Type RowDefinition}">
                                        <Setter Property="Height" Value="0"/>

                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding SprintTitleOrientation}" Value="Horizontal">
                                                <Setter Property="Height" Value="2*"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </RowDefinition.Style>
                            </RowDefinition>
                            <RowDefinition>
                                <RowDefinition.Style>
                                    <Style TargetType="{x:Type RowDefinition}">
                                        <Setter Property="Height" Value="5*"/>

                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding SprintMode}" Value="Planning">
                                                <Setter Property="Height" Value="Auto"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </RowDefinition.Style>
                            </RowDefinition>
                            <RowDefinition Height="4*" />
                        </Grid.RowDefinitions>

                        <!-- Horizontal sprint title region -->
                        <ItemsControl Grid.Row="0" Grid.ColumnSpan="2" x:Name="HorizontalSprintTitleRegion" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.HorizontalSprintTitleRegion}" Focusable="False">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Grid />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>

                        <!-- Vertical sprint title region -->
                        <ItemsControl Grid.Row="0" Grid.RowSpan="3" x:Name="VerticalSprintTitleRegion" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.VerticalSprintTitleRegion}" Focusable="False">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Grid />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>

                        <ItemsControl Grid.Row="1" Grid.Column="1" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.SprintOverviewChartsRegion}" Focusable="False">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid Columns="{Binding Path=ItemsSource.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" IsItemsHost="True" HorizontalAlignment="Stretch" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>

                        <Viewbox Grid.Row="2" Grid.Column="1">
                            <ItemsControl cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.SprintStatisticsRegion}" Focusable="False">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Grid.IsSharedSizeScope="True"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </Viewbox>
                    </Grid>

                    <!-- Right column -->
                    <ItemsControl Grid.Column="2" cal:RegionManager.RegionName="{x:Static Extensibility:RegionNames.PrimaryMonitorRightDockRegion}" Focusable="False">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </Grid>
            </DockPanel>
        </Grid>
    </DataTemplate>

    <DataTemplate DataType="{x:Type ViewModels:MonitorViewModel}">
        <Grid>
            <ContentPresenter Content="{Binding}" Focusable="False">
                <ContentPresenter.Style>
                    <Style TargetType="{x:Type ContentPresenter}">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate />
                            </Setter.Value>
                        </Setter>

                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=ActiveView}" Value="SprintView">
                                <Setter Property="ContentTemplate" Value="{StaticResource OnlineDataTemplate}" />
                            </DataTrigger>
                            
                            <DataTrigger Binding="{Binding Path=ActiveView}" Value="ExceptionView">
                                <Setter Property="ContentTemplate" Value="{StaticResource ErrorDataTemplate}" />
                            </DataTrigger>

                            <DataTrigger Binding="{Binding Path=ActiveView}" Value="FoundUnassignedWorkItems">
                                <Setter Property="ContentTemplate" Value="{StaticResource WorkItemNotEstimatedErrorDataTemplate}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentPresenter.Style>
            </ContentPresenter>

            <ContentPresenter Panel.ZIndex="1000" Content="{Binding}" Focusable="False" ContentTemplate="{StaticResource FetchingWorkItemsDataTemplate}">
                <ContentPresenter.Style>
                    <Style TargetType="{x:Type ContentPresenter}">
                        <Setter Property="Visibility" Value="Hidden" />
                        <Setter Property="Opacity" Value="0" />

                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ShowProgress}" Value="True">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            
                                            <DoubleAnimation Duration="00:00:01" Storyboard.TargetProperty="Opacity" To="1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>

                                <DataTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Duration="00:00:01" Storyboard.TargetProperty="Opacity" To="0" />
                                            
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:01">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Hidden</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentPresenter.Style>
            </ContentPresenter>
        </Grid>
    </DataTemplate>
</ResourceDictionary>